home *** CD-ROM | disk | FTP | other *** search
/ Into His Marvelous Light / Into His Marvelous LIGHT.iso / lesson2.dxr / 00324_Script_Color Cycling < prev    next >
Text File  |  1998-01-03  |  10KB  |  247 lines

  1. -- Color Cycling
  2. -- Animates the color of a sprite over time.
  3. -- v1 by Darrel Plant
  4.  
  5. on getBehaviorDescription
  6.   vDesc = "COLOR CYCLING" & RETURN & RETURN
  7.   vDesc = vDesc & "Use this behavior to create a color animation effect. The"
  8.   vDesc = vDesc && "sprite will change over a period of time, from one color"
  9.   vDesc = vDesc && "to another. Use color indices from the movie palette or"
  10.   vDesc = vDesc && "RGB color values. The speed of the color cycle can be set,"
  11.   vDesc = vDesc && "as can the number of time the colors cycle. To make the"
  12.   vDesc = vDesc && "color change once (from the start color to the end color)"
  13.   vDesc = vDesc && "set the number of cycles to 0, choose -1 to cycle forever."
  14.   vDesc = vDesc  & RETURN& RETURN & "PARAMETERS:" & RETURN
  15.   vDesc = vDesc & " - Color mode (RGB or palette index)"
  16.   vDesc = vDesc & RETURN & " - Time period (seconds) for each change of colors"
  17.   vDesc = vDesc & RETURN & " - Number of cycles" & RETURN
  18.   vDesc = vDesc & " - Settings for start color" & RETURN
  19.   vDesc = vDesc & " - Settings for end color"
  20.   return vDesc
  21. end getBehaviorDescription
  22.  
  23. on getBehaviorTooltip me
  24.   vTip ="Cycles a sprite's colors through either the"&RETURN
  25.   vTip = vTip & "RGB or Indexed color mode.  Cycling can be"&RETURN
  26.   vTip = vTip & "continuous or for a specific number of cycles."&RETURN
  27.   vTip = vTip & "The cycle rate can also be set."
  28.   return vTip
  29. end getBehaviorTooltip
  30.  
  31. -- PROPERTIES --
  32.  
  33. property pSprite                      -- sprite object reference
  34. property pColorStart                  -- color object for start of cycle
  35. property pColorEnd                    -- color object for end of cycle
  36. property pCycleStart                  -- time current color change started
  37. property pCycleEnd                    -- time current color change ends
  38. property pColorRDiff                  -- difference in start and end red
  39. --                                    --   or palette index values
  40. property pColorGDiff                  -- difference in green values
  41. property pColorBDiff                  -- differencein blue values
  42. property pCycleMillis                 -- color change period in milliseconds
  43. property pCyclesComplete              -- number of complete color cycles
  44. -- author-defined properties
  45. property pColorMode                   -- flag for RGB or palette index
  46. property pCyclePeriod                 -- color change period in seconds
  47. property pColorCycles                 -- number of color cycles to complete
  48. property pColor1R                     -- red component or palette index
  49. --                                    --   of start color
  50. property pColor1G                     -- green component of start color
  51. property pColor1B                     -- blue component of start color
  52. property pColor2R                     -- red component or palette index
  53. --                                    --   of end color
  54. property pColor2G                     -- green component of end color
  55. property pColor2B                     -- blue component of end color
  56.  
  57. -- EVENT HANDLERS --
  58.  
  59. on beginSprite me
  60.   mInitialize me
  61. end beginSprite
  62.  
  63. on prepareFrame me
  64.   mUpdate me
  65. end prepareFrame
  66.  
  67. -- CUSTOM HANDLERS --
  68.  
  69. on mInitialize me
  70.   -- cache sprite object
  71.   pSprite = sprite (me.spriteNum)
  72.   -- initialize number of cycles
  73.   pCyclesComplete = 0.5
  74.   -- convert author-set period to milliseconds
  75.   pCycleMillis = integer (pCyclePeriod * 1000)
  76.   -- set up first color change
  77.   mInitColors me
  78. end mInitialize
  79.  
  80. on mUpdate me
  81.   -- determine current time
  82.   vTime = the milliseconds
  83.   -- determine time elapsed in current color change
  84.   vElapsed = vTime - pCycleStart
  85.   -- check whether color change should be complete
  86.   if vTime >= pCycleEnd then
  87.     -- set sprite color to color at end of change
  88.     pSprite.color = pColorEnd
  89.     -- increment cycle counter
  90.     pCyclesComplete = pCyclesComplete + 0.5
  91.     -- start new color change, depending on
  92.     -- setting for number of cycles
  93.     case pColorCycles of
  94.         -- continuous cycling, automatically start change
  95.       -1: mInitColors me
  96.         -- single color change, no more changes
  97.       0: nothing
  98.       otherwise
  99.         -- a limited number of cycles requires a test
  100.         -- so see whether the required cycles have been
  101.         -- completed, if not, then start new change
  102.         if pCyclesComplete <= pColorCycles then
  103.           mInitColors me
  104.         end if
  105.     end case
  106.   else
  107.     -- determine whether colors are set using RGB or palette index values
  108.     if pColorMode = #rgb then
  109.       -- determine current color component values based on percentage of
  110.       -- time allocated to change that has elapsed multiplied by the
  111.       -- difference between the start and end color components for
  112.       -- red, green, and blue
  113.       vRed = pColorStart.red + pColorRDiff * vElapsed / pCycleMillis
  114.       vGreen = pColorStart.green + pColorGDiff * vElapsed / pCycleMillis
  115.       vBlue = pColorStart.blue + pColorBDiff * vElapsed / pCycleMillis
  116.       -- create RGB color object from current color component values and
  117.       -- assign to sprite
  118.       pSprite.color = rgb (vRed, vGreen, vBlue)
  119.     else
  120.       -- palette index color mode uses only one of the color values,
  121.       -- ignoring whatever settings may exist for green and blue
  122.       vRed = pColorStart.paletteIndex + pColorRDiff * vElapsed / pCycleMillis
  123.       -- create palette index color object from current color component values
  124.       -- and assign to sprite
  125.       pSprite.color = paletteIndex (vRed)
  126.     end if
  127.   end if
  128. end mUpdate
  129.  
  130. on mInitColors me
  131.   -- creates a new color change by deciding which portion off the color
  132.   -- cycle is being represented, setting up start and end colors, and
  133.   -- establishing timing information
  134.   -- when the pCompletedCycles property is 0.5, 1.5, 2.5, etc., this
  135.   -- test evaluates TRUE
  136.   if integer (pCyclesComplete * 2) <> integer (pCyclesComplete) * 2 then
  137.     -- change color from first color to second color
  138.     vStartColorList = [pColor1R, pColor1G, pColor1B]
  139.     vEndColorList = [pColor2R, pColor2G, pColor2B]
  140.   else
  141.     -- change color from second color to first color
  142.     vStartColorList = [pColor2R, pColor2G, pColor2B]
  143.     vEndColorList = [pColor1R, pColor1G, pColor1B]
  144.   end if
  145.   -- set up color objects for start and end colors, depending on setting
  146.   -- for the color mode
  147.   if pColorMode = #rgb then
  148.     -- create RGB color objects
  149.     pColorStart = ¼
  150.       rgb (vStartColorList[1], vStartColorList[2], vStartColorList[3])
  151.     pColorEnd = rgb (vEndColorList[1], vEndColorList[2], vEndColorList[3])
  152.   else
  153.     -- create palette index color objects
  154.     pColorStart = paletteIndex (vStartColorList[1])
  155.     pColorEnd = paletteIndex (vEndColorList[1])
  156.   end if
  157.   -- determine differences in color components
  158.   pColorRDiff = vEndColorList [1] - vStartColorList[1]
  159.   pColorGDiff = vEndColorList [2] - vStartColorList[2]  
  160.   pColorBDiff = vEndColorList [3] - vStartColorList[3]
  161.   -- set time color change began
  162.   pCycleStart = the milliseconds
  163.   -- add cycle period to determine when change should end
  164.   pCycleEnd = pCycleStart + pCycleMillis
  165.   -- set the sprite to the current start color
  166.   pSprite.color = pColorStart
  167. end mInitColors  
  168.  
  169. -- ERROR CHECKING --
  170.  
  171. on mErrorAlert me, vError, vData
  172.   -- based on James Newton's error checking procedure
  173.   -- determine name of behavior
  174.   vBehaviorname = string (me)
  175.   delete word 1 of vBehaviorName
  176.   delete the last word of vBehaviorName
  177.   delete the last word of vBehaviorName
  178.   -- convert supporting data
  179.   case vData.ilk of
  180.     #void: vData = "<void>"
  181.     #symbol: vData = "#" & vData
  182.   end case
  183.   case vError of
  184.       -- deal with individual error types
  185.     #getPDLError:  -- error from property dialog
  186.       beep
  187.       return [#getPDLError: [#comment:  ¼
  188.         "CANCEL: Sprite MUST be one of the following member types:" ¼
  189.          & RETURN & vData, #format: #symbol,  ¼
  190.          #range: [#Cancel], #default: #Cancel]]
  191.   end case
  192. end mErrorAlert
  193.  
  194. -- AUTHOR-DEFINED PARAMETERS --
  195.  
  196. on getPropertyDescriptionList me
  197.   if not the currentSpriteNum then
  198.     -- behavior has been attached to script channel
  199.     exit
  200.   end if
  201.   vRect = the stage.rect
  202.   vMemberType = sprite (the currentSpriteNum).member.type
  203.   if not getPos (mPermittedMemberTypes (), vMemberType) then
  204.     return mErrorAlert (me, #getPDLError)
  205.   end if
  206.   vColor = sprite (the currentSpriteNum).color
  207.   vColorType = vColor.colorType
  208.   if vColorType = #rgb then
  209.     vRed = vColor.red
  210.     vGreen = vColor.green
  211.     vBlue = vColor.blue
  212.   else
  213.     vRed = vColor.paletteIndex
  214.     vGreen = 0
  215.     vBlue = 0
  216.   end if
  217.   vPDList = [:]
  218.   setaProp vPDList, #pColorMode, [#comment: "Color mode", ¼
  219.     #format: #symbol, #default: #palette, #range: [#palette, #RGB]]
  220.   setaProp vPDList, #pCyclePeriod, [#comment: "Cycle period (seconds)", ¼
  221.     #format: #float, #default: 3, #range: [#min: 1, #max: 25]]
  222.   setaProp vPDList, #pColorCycles, [#comment: "Color cycles", ¼
  223.     #format: #integer, #default: 0, #range: [#min: -1, #max: 100]]
  224.   setaProp vPDList, #pColor1R, ¼
  225.     [#comment: "Start color: palette index or Red component", ¼
  226.     #format: #integer, #default: vRed, #range: [#min: 0, #max: 255]]
  227.   setaProp vPDList, #pColor1G, ¼
  228.     [#comment: "Green component", ¼
  229.     #format: #integer, #default: vGreen, #range: [#min: 0, #max: 255]]
  230.   setaProp vPDList, #pColor1B, ¼
  231.     [#comment: "Blue component", ¼
  232.     #format: #integer, #default: vBlue, #range: [#min: 0, #max: 255]]
  233.   setaProp vPDList, #pColor2R, ¼
  234.     [#comment: "Final color: palette index or Red component", ¼
  235.     #format: #integer, #default: 255 - vRed, #range: [#min: 0, #max: 255]]
  236.   setaProp vPDList, #pColor2G, ¼
  237.     [#comment: "Green component", ¼
  238.     #format: #integer, #default: 255 - vGreen, #range: [#min: 0, #max: 255]]
  239.   setaProp vPDList, #pColor2B, ¼
  240.     [#comment: "Blue component", ¼
  241.     #format: #integer, #default: 255 - vBlue, #range: [#min: 0, #max: 255]]
  242.   return vPDList
  243. end getPropertyDescriptionList
  244.  
  245. on mPermittedMemberTypes me
  246.   return [#bitmap, #flash, #animgif, #shape, #text]
  247. end mPermittedMemberTypes